package cc.grapevine.npaddemo.util;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import cc.grapevine.npaddemo.data.OptionData;
import cc.grapevine.npaddemo.data.QuestionData;

public class QuestonUtil
{
	public static List<QuestionData> getQuestion(InputStream in)
	{
		List<QuestionData> questionDatas = new ArrayList<QuestionData>();

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try
		{
			DocumentBuilder builder = factory.newDocumentBuilder();
			// Document dom = builder.parse(new URL(url).openStream());
			Document dom = builder.parse(in);

			Element root = dom.getDocumentElement();
			NodeList items = root.getElementsByTagName("question");

			for (int i = 0; i < items.getLength(); i++)
			{
				QuestionData questionData = new QuestionData();

				Node item = items.item(i);
				NamedNodeMap datas = item.getAttributes();
				int lens = datas.getLength();
				for (int p = 0; p < lens; p++)
				{
					Node d = datas.item(p);

					String names = d.getNodeName();

					if ("id".equals(names))
					{
						questionData.setId(d.getNodeValue());
					}
					else if ("answer".equals(names))
					{
						questionData.setAnswer(d.getNodeValue());
					}
					else if ("desc".equals(names))
					{
						questionData.setDesc(d.getNodeValue());
					}
					else if ("title".equals(names))
					{
						questionData.setTitle(d.getNodeValue());
					}
				}

				NodeList properties = item.getChildNodes();

				lens = properties.getLength();

				List<OptionData> optionDatas = new ArrayList<OptionData>();
				for (int j = 0; j < lens; j++)
				{

					Node property = properties.item(j);
					
					if(property.getNodeName().equalsIgnoreCase("option")){
					
						NamedNodeMap data = property.getAttributes();

						OptionData optionData = new OptionData();

						int len = data.getLength();

						for (int k = 0; k < len; k++)
						{
							Node node = data.item(k);

							String attrName = node.getNodeName();

							if (attrName.equalsIgnoreCase("id"))
							{
								optionData.setId(node.getNodeValue());
							}
							else if (attrName.equalsIgnoreCase("title"))
							{
								optionData.setTitle(node.getNodeValue());
							}
							else if (attrName.equalsIgnoreCase("image"))
							{
								optionData.setImage(node.getNodeValue());
							}
							else if (attrName.equalsIgnoreCase("audio"))
							{
								optionData.setAudio(node.getNodeValue());
							}
							else if (attrName.equalsIgnoreCase("video"))
							{
								optionData.setVideo(node.getNodeValue());
							}
						}
						optionDatas.add(optionData);
						
					}
					
				}
				questionData.setOptionList(optionDatas);
				questionDatas.add(questionData);
			}
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}

		return questionDatas;
	}
}